package com.freshmarket.freshmarket.exception;


import com.freshmarket.freshmarket.utils.Result;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler()
    @ResponseBody
    public Result tryPrintException(Exception e) {
        if(e instanceof org.springframework.validation.BindException){
            e.printStackTrace();
            return Result.failedResult(000,"您的发送参数异常");
        }else if(e instanceof UnknownAccountException || e instanceof IncorrectCredentialsException){
            return Result.failedResult(000,"错误的用户或密码");
        }else if(e instanceof UnauthorizedException){
            return Result.failedResult(000,"无权限的操作");
        }else if(e instanceof ArgsMistakeException){
            return Result.failedResult(000,e.getMessage());
        }else if(e instanceof RepetitiveProductException){
            return Result.failedResult(000,"已存在的商品");
        }else{
            e.printStackTrace();
            return Result.failedResult(000, "服务器出现意料之外的错误");
        }
    }
}
